<?php
/**
 * 请假管理
 * @author Administrator
 */
class leave extends admin{
    public function __construct(){
        parent::__construct();
    }
    
    //教练请假列表
    public function index(){
        $school_id=(int)$_SESSION['client']['jx_id'];
        $where='where school_id='.$school_id;
        if(isset($_GET['k'])&&$_GET['k']!='') {
            $k=trim($_GET['k']);
            $map='where user_type=2 ';
            if(preg_match('/^1[3-9][0-9]{9}$/',$k)) $map.=' and mobile='.$k;
            else $map.=' and real_name like "%'.$k.'%"';
            $id_list=db()->getAll('select id from x_user '.$map);
            $ids='';
            if($id_list){
                foreach ($id_list as $v){
                    $ids.=$v['id'].',';
                }
                $ids=trim($ids,',');
            }
            $where.=' and uid in('.$ids.') ';
        }else{
            $where.=' and status=0 ';
        }
        $total=db()->getOne('select count(*) from x_leave_record '.$where);
        if(isset($_GET['pagesize'])&&(int)$_GET['pagesize']>0) $pagesize=(int)$_GET['pagesize'];
        else $pagesize=C('PAGE_SIZE');
        if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
        else $p = 1;
        $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
        $list=db()->getAll('select id,uid,apply_time,begin_time,end_time,status from x_leave_record '.$where.' order by id desc '.$limit);
        if($list){
            foreach($list as $k=>$v){
                $u=db()->getRow('select real_name,mobile,sex from x_user where id='.$v['uid']);
                if($u){
                    $list[$k]['real_name']=$u['real_name'];
                    $list[$k]['mobile']=$u['mobile'];
                    $list[$k]['sex']=$u['sex'];
                }
                    $diff=$v['end_time']-$v['begin_time'];
                    $d=(int)($diff/86400);
                    $h=(int)($diff%86400)/3600;
                    $str='';
                    if($d>0) $str=$d.'天';
                    if($h>0) $str.=$h.'小时';
                $list[$k]['duration']=$str;
            }
        }else $list=array();
        $page = array('total' => $total,'pagesize' =>$pagesize,'curpage' => $p);
        $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
    }
    
    //教练请假审核
    public function audit(){
            $id=(int)$_POST['id'];
            if((int)$id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数id'));
            $data['status']=(int)$_POST['status'];
            //$data['remark']=remove_xss($_POST['remark']);
            $data['audit_time']=NOW;
            $affert_num=db()->update('x_leave_record',$data,'id='.$id);
            if($affert_num>0) {
                insertlogs('update','教练请假审核',$data,$id);
                $leave=db()->getRow('select uid,begin_time,end_time from x_leave_record where id='.$id.' limit 1');
                if($leave){
                    db()->update('x_coach_book',array('status'=>3),'coach_id='.$leave['uid'].' and status=0 and class_time between '.$leave['begin_time'].' and '.$leave['end_time']);
                }
                $this->ajaxReturn(array('status'=>1,'info'=>'操作成功'));
            }
            else $this->ajaxReturn(array('status'=>0,'info'=>'操作失败'));
    }
    
}